package cn.dlc.com.base.controller;

import cn.dlc.com.base.dto.BaseDTO;
import cn.dlc.com.base.entity.BaseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

/**
 * 通用Controller
 */
public class BaseController<T extends BaseEntity, E extends BaseDTO> {

    /**
     * 构造排序
     * @param queryWrapper 排序构造器
     * @param dto 排序参数
     */
    public void buildQueryWrapper(QueryWrapper<T> queryWrapper, E dto){
        if(dto != null) {
            if(StringUtils.isNotBlank(dto.getColumns()) && StringUtils.isNotBlank(dto.getDescTypes())) {
                String[] columns = dto.getColumns().split(",");
                String[] descTypes = dto.getDescTypes().split(",");
                for (int i = 0; i < columns.length; i++) {
                    if("desc".equals(descTypes[i].toLowerCase().trim())) {
                        queryWrapper.orderByDesc(columns[i]);
                    } else if("asc".equals(descTypes[i].toLowerCase().trim())) {
                        queryWrapper.orderByAsc(columns[i]);
                    }
                }
            }
        }
    }
}
